<?php

namespace App\Http\Controllers\V1\Admin;


use App\Models\V1\Community;
use App\Models\V1\Goods;
use Illuminate\Http\Request;
use App\Http\Requests\Admin\GoodsServer;


class GoodsController extends BaseController
{

    /**
     * 兑换物品列表
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Goods::query();
        $pageSize = $request->input('pageSize', 10);
        $order = $request->input('order', 'desc');
        $sort = $request->input('sort', 'id');
        if ($request->filled('nickname')) {
            $query->where('nickname', 'like', "%" . $request->input('nickname', "") . "%");
        }
        if ($request->filled('mobile')) {
            $query->where('mobile', $request->input('mobile'));
        }
        if ($request->filled('community_id')) {
            $query->where('community_id', $request->input('community_id'));
        }

        $query->orderBy($sort, $order);
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 增加兑换物品
     *
     * @param GoodsServer $request
     * @return \Illuminate\Http\Response
     */
    public function store(GoodsServer $request)
    {
        $request->validate('v1.create');
        $community_id = $request->input('community_id', "");
        if ($community_id) {
            $community_name = Community::where('id', $community_id)->value('name');
        } else {
            $community_name = '';
        }
        $model = new Goods();
        $model->create([
            'title' => $request->input('title'),
            'thumb' => $request->input('thumb', ""),
            'integral' => $request->input('integral', ""),
            'particulars' => $request->input('particulars', ""),
            'community_id' => $community_id,
            'community_name' => $community_name,
            'state' => $request->input('state', 1),
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 兑换物品详情
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $vo = Goods::find($id);
        return ajax_output(200, "成功", $vo);
    }


    /**
     * 更新兑换物品
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $vo = Goods::find($id);
        $community_id = $request->input('community_id', "");
        if ($community_id) {
            $community_name = Community::where('id', $community_id)->value('name');
        } else {
            $community_name = '';
        }
        $vo->update([
            'title' => $request->input('title'),
            'thumb' => $request->input('thumb', ""),
            'integral' => $request->input('integral', ""),
            'particulars' => $request->input('particulars', ""),
            'community_id' => $community_id,
            'community_name' => $community_name,
            'state' => $request->input('state', 1),
        ]);
        return ajax_output(200, "成功");
    }

    /**
     * 删除兑换物品
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $id = explode(',',$id);
        Goods::destroy($id);
        return ajax_output(200, "成功");
    }
}
